<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 作业
        // 写一个函数，接收任意一个字符串，算出这个字符串的总字节数

        function totalAscii(str){
            var total = 0;
            var arr = str.split('');
            for(var i = 0; i < arr.length; i++){
                var code = arr[i].charCodeAt(0)
                if(code && code > 0) {
                    if(code <= 255){
                        total += 1;
                    } else {
                        total += 2;
                    }
                }
            }
            return total;
        }

        var total = totalAscii('ab中文');
        console.log(total);
    </script>
</body>
</html>